package com.dianping.cat.consumer.event.model.transform;

import com.dianping.cat.consumer.event.model.IVisitor;
import com.dianping.cat.consumer.event.model.entity.EventName;
import com.dianping.cat.consumer.event.model.entity.EventReport;
import com.dianping.cat.consumer.event.model.entity.EventType;
import com.dianping.cat.consumer.event.model.entity.GraphTrend;
import com.dianping.cat.consumer.event.model.entity.Machine;
import com.dianping.cat.consumer.event.model.entity.Range;
import com.dianping.cat.consumer.event.model.entity.StatusCode;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;


public class DefaultNativeBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private DataOutputStream m_out;

  public DefaultNativeBuilder(OutputStream out) { this(out, null); }


  public DefaultNativeBuilder(OutputStream out, IVisitor visitor) {
/*  28 */     this.m_out = new DataOutputStream(out);
    this.m_visitor = (visitor == null) ? this : visitor;
  }

  public static byte[] build(EventReport eventReport) {
/*  33 */     ByteArrayOutputStream out = new ByteArrayOutputStream(8192);

/*  35 */     build(eventReport, out);
    return out.toByteArray();
  }


/*  40 */   public static void build(EventReport eventReport, OutputStream out) { eventReport.accept(new DefaultNativeBuilder(out)); }



  public void visitEventReport(EventReport eventReport) {
/*  45 */     writeTag(63, 0);

/*  47 */     if (eventReport.getDomain() != null) {
/*  48 */       writeTag(1, 1);
/*  49 */       writeString(eventReport.getDomain());
    }

/*  52 */     if (eventReport.getStartTime() != null) {
/*  53 */       writeTag(2, 1);
/*  54 */       writeDate(eventReport.getStartTime());
    }

/*  57 */     if (eventReport.getEndTime() != null) {
/*  58 */       writeTag(3, 1);
/*  59 */       writeDate(eventReport.getEndTime());
    }

/*  62 */     if (eventReport.getDomainNames() != null) {
/*  63 */       writeTag(4, 2);
/*  64 */       writeInt(eventReport.getDomainNames().size());

/*  66 */       for (String domain : eventReport.getDomainNames()) {
/*  67 */         writeString(domain);
      }
    }

/*  71 */     if (eventReport.getIps() != null) {
/*  72 */       writeTag(5, 2);
/*  73 */       writeInt(eventReport.getIps().size());

/*  75 */       for (String ip : eventReport.getIps()) {
/*  76 */         writeString(ip);
      }
    }

/*  80 */     if (!eventReport.getMachines().isEmpty()) {
/*  81 */       writeTag(33, 2);
/*  82 */       writeInt(eventReport.getMachines().size());

/*  84 */       for (Machine machine : eventReport.getMachines().values()) {
/*  85 */         machine.accept(this.m_visitor);
      }
    }

/*  89 */     writeTag(63, 3);
  }


  public void visitGraphTrend(GraphTrend graphTrend) {
/*  94 */     writeTag(1, 0);
/*  95 */     writeInt(graphTrend.getDuration());

/*  97 */     writeTag(2, 0);
/*  98 */     writeString(graphTrend.getCount());

/* 100 */     writeTag(3, 0);
/* 101 */     writeString(graphTrend.getFails());

/* 103 */     writeTag(63, 3);
  }


  public void visitMachine(Machine machine) {
/* 108 */     if (machine.getIp() != null) {
/* 109 */       writeTag(1, 1);
/* 110 */       writeString(machine.getIp());
    }

/* 113 */     if (!machine.getTypes().isEmpty()) {
/* 114 */       writeTag(33, 2);
/* 115 */       writeInt(machine.getTypes().size());

/* 117 */       for (EventType type : machine.getTypes().values()) {
/* 118 */         type.accept(this.m_visitor);
      }
    }

/* 122 */     writeTag(63, 3);
  }


  public void visitName(EventName name) {
/* 127 */     if (name.getId() != null) {
/* 128 */       writeTag(1, 1);
/* 129 */       writeString(name.getId());
    }

/* 132 */     writeTag(2, 0);
/* 133 */     writeLong(name.getTotalCount());

/* 135 */     writeTag(3, 0);
/* 136 */     writeLong(name.getFailCount());

/* 138 */     writeTag(4, 0);
/* 139 */     writeDouble(name.getFailPercent());

/* 141 */     if (name.getSuccessMessageUrl() != null) {
/* 142 */       writeTag(5, 1);
/* 143 */       writeString(name.getSuccessMessageUrl());
    }

/* 146 */     if (name.getFailMessageUrl() != null) {
/* 147 */       writeTag(6, 1);
/* 148 */       writeString(name.getFailMessageUrl());
    }

/* 151 */     writeTag(7, 0);
/* 152 */     writeDouble(name.getTps());

/* 154 */     if (!name.getRanges().isEmpty()) {
/* 155 */       writeTag(33, 2);
/* 156 */       writeInt(name.getRanges().size());

/* 158 */       for (Range range : name.getRanges().values()) {
/* 159 */         range.accept(this.m_visitor);
      }
    }

/* 163 */     if (name.getGraphTrend() != null) {
/* 164 */       writeTag(34, 1);
/* 165 */       name.getGraphTrend().accept(this.m_visitor);
    }

/* 168 */     if (!name.getStatusCodes().isEmpty()) {
/* 169 */       writeTag(35, 2);
/* 170 */       writeInt(name.getStatusCodes().size());

/* 172 */       for (StatusCode statusCode : name.getStatusCodes().values()) {
/* 173 */         statusCode.accept(this.m_visitor);
      }
    }

/* 177 */     writeTag(63, 3);
  }


  public void visitRange(Range range) {
/* 182 */     if (range.getValue() != null) {
/* 183 */       writeTag(1, 1);
/* 184 */       writeInt(range.getValue().intValue());
    }

/* 187 */     writeTag(2, 0);
/* 188 */     writeInt(range.getCount());

/* 190 */     writeTag(3, 0);
/* 191 */     writeInt(range.getFails());

/* 193 */     writeTag(63, 3);
  }


  public void visitStatusCode(StatusCode statusCode) {
/* 198 */     if (statusCode.getId() != null) {
/* 199 */       writeTag(1, 1);
/* 200 */       writeString(statusCode.getId());
    }

/* 203 */     writeTag(2, 0);
/* 204 */     writeLong(statusCode.getCount());

/* 206 */     writeTag(63, 3);
  }


  public void visitType(EventType type) {
/* 211 */     if (type.getId() != null) {
/* 212 */       writeTag(1, 1);
/* 213 */       writeString(type.getId());
    }

/* 216 */     writeTag(2, 0);
/* 217 */     writeLong(type.getTotalCount());

/* 219 */     writeTag(3, 0);
/* 220 */     writeLong(type.getFailCount());

/* 222 */     writeTag(4, 0);
/* 223 */     writeDouble(type.getFailPercent());

/* 225 */     if (type.getSuccessMessageUrl() != null) {
/* 226 */       writeTag(5, 1);
/* 227 */       writeString(type.getSuccessMessageUrl());
    }

/* 230 */     if (type.getFailMessageUrl() != null) {
/* 231 */       writeTag(6, 1);
/* 232 */       writeString(type.getFailMessageUrl());
    }

/* 235 */     writeTag(7, 0);
/* 236 */     writeDouble(type.getTps());

/* 238 */     if (!type.getNames().isEmpty()) {
/* 239 */       writeTag(33, 2);
/* 240 */       writeInt(type.getNames().size());

/* 242 */       for (EventName name : type.getNames().values()) {
/* 243 */         name.accept(this.m_visitor);
      }
    }

/* 247 */     if (type.getGraphTrend() != null) {
/* 248 */       writeTag(34, 1);
/* 249 */       type.getGraphTrend().accept(this.m_visitor);
    }

/* 252 */     writeTag(63, 3);
  }

  private void writeDate(Date value) {
    try {
/* 257 */       writeVarint(value.getTime());
/* 258 */     } catch (Exception e) {
/* 259 */       throw new RuntimeException(e);
    }
  }

  private void writeDouble(double value) {
    try {
/* 265 */       writeVarint(Double.doubleToLongBits(value));
/* 266 */     } catch (Exception e) {
/* 267 */       throw new RuntimeException(e);
    }
  }

  private void writeInt(int value) {
    try {
/* 273 */       writeVarint(value & 0xFFFFFFFFL);
/* 274 */     } catch (Exception e) {
/* 275 */       throw new RuntimeException(e);
    }
  }

  private void writeLong(long value) {
    try {
/* 281 */       writeVarint(value);
/* 282 */     } catch (Exception e) {
/* 283 */       throw new RuntimeException(e);
    }
  }

  private void writeString(String value) {
    try {
/* 289 */       this.m_out.writeUTF(value);
/* 290 */     } catch (IOException e) {
/* 291 */       throw new RuntimeException(e);
    }
  }

  private void writeTag(int field, int type) {
    try {
/* 297 */       this.m_out.writeByte((field << 2) + type);
/* 298 */     } catch (IOException e) {
/* 299 */       throw new RuntimeException(e);
    }
  }

  protected void writeVarint(long value) throws IOException {
    while (true) {
/* 305 */       if ((value & 0xFFFFFFFFFFFFFF80L) == 0L) {
/* 306 */         this.m_out.writeByte((byte)(int)value);
        return;
      }
/* 309 */       this.m_out.writeByte((byte)(int)value & 0x7F | 0x80);
/* 310 */       value >>>= 7;
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/event/model/transform/DefaultNativeBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */